<template>
    <div class="login">
      <div class="login-box">
        <div class="img-box">
          <img src="../assets/img/avatar.jpg"/>
        </div>
        <div class="login-form">
          <el-form ref="loginFromRef" :model="loginForm" :rules="loginFormRules" label-width="0px">
            <!--用户名-->
            <el-form-item prop="username">
              <el-input prefix-icon="iconfont icon-user"  v-model="loginForm.username"></el-input>
            </el-form-item>
            <!--密码-->
            <el-form-item prop="password">
              <el-input type="password" prefix-icon="iconfont icon-3702mima"  v-model="loginForm.password"></el-input>
            </el-form-item>
           <!-- 按钮-->
            <el-form-item class="btns">
              <el-button type="primary" @click="login">登录</el-button>
              <el-button type="info" @click="resetLoginFrom">重置</el-button>
            </el-form-item>
          </el-form>
        </div>
      </div>
    </div>
</template>

<script>
    export default {
        name: "Login",
      data(){
          return{
            loginForm:{
              username:'admin',
              password:'123456'
            },
            loginFormRules:{
              //验证用户名
              username:[
                { required: true, message: '请输入用户名', trigger: 'blur' },
                { min: 3, max: 10, message: '长度在 3 到 10 个字符', trigger: 'blur' }
              ],
              //验证密码
              password:[
                { required: true, message: '请输入登录密码', trigger: 'blur' },
                { min: 6, max: 15, message: '长度在 6到 15 个字符', trigger: 'blur' }
              ]
            }
          }
      },
      methods:{
        //重置
        resetLoginFrom(){
           this.$refs.loginFromRef.resetFields();
        },
        //登录
        login(){
          this.$refs.loginFromRef.validate((valid)=>{
            if(!valid){
              return
            }else{
              this.$http.post('login',this.loginForm).then(res=>{
                if(res.data.meta.status!==200){
                  this.$message({
                    message: '登录失败',
                    type: 'warning',
                    offset:'80'
                  });
                  return
                }else{
                  //登录成功
                  window.sessionStorage.setItem('token',res.data.data.token);
                  this.$router.push('/home');
                }
              })
            }
          })
        }
      }
    }
</script>

<style lang="less" scoped>
.login{
  height: 100%;
  background-color: #1b6d85;
}
  .login-box{
    width: 430px;
    height: 300px;
    background-color: #fff;
    border-radius: 4px;
    position: absolute;
    left: 50%;
    top:50%;
    transform: translate(-50%,-50%);
    .img-box{
      height: 130px;
      width: 130px;
      position: absolute;
      left: 50%;
      top: -65px;
      transform: translate(-50%);
      border: 1px solid #eee;
      border-radius: 50%;
      padding: 5px;
      background-color: #eee;
      box-shadow: 0 0 10px #ddd;
      img{
        width: 100%;
        height: 100%;
        border-radius: 50%;
      }
    }
    .login-form{
      margin-top: 100px;
      padding: 0 20px;
    }
  }
  .btns{
    display: flex;
    justify-content: flex-end;
  }
</style>

